#!/bin/sh
# LambdaNative - a cross-platform Scheme framework
# Copyright (c) 2009-2014, University of British Columbia
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the
# following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# * Neither the name of the University of British Columbia nor
# the names of its contributors may be used to endorse or
# promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

bb10_subst()
{
  ac_subst BB10_C_DEFINES "@$1/BB10_c_defines"
  ac_subst BB10_XML_PERMISSIONS "@$1/BB10_xml_permissions"
  ac_subst BB10_XML_ASSETS "@$1/BB10_xml_assets"
}
bb10_subst loaders/qnx
for m in $modules; do
  modpath=`locatedir modules/$m silent`
  bb10_subst $modpath
done
bb10_subst `locatedir apps/$SYS_APPNAME`

configsrc=`pwd`"/loaders/qnx/bb10-bar-descriptor.xml.in"
assertfile $configsrc "BB10 configuration desriptor is required"
mkdir -p $SYS_PREFIXROOT/build/$SYS_APPNAME
configtgt=$SYS_PREFIXROOT/build/$SYS_APPNAME/bar-descriptor.xml
tmpdir=`mktemp -d $SYS_TMPDIR/tmp.XXXXXX`
if [ `is_gui_app` = yes ]; then
  ac_output loaders/qnx/bootstrap.c.in $tmpdir/bootstrap.c
else
  if [ `is_standalone_app` = "no" ]; then
    cp loaders/common/main.c $tmpdir
  fi
fi
cd $tmpdir

echo " => compiling application.."
tgt=$SYS_APPNAME$SYS_EXEFIX
if [ `is_standalone_app` = "yes" ]; then
  touch main.c
  veval "$SYS_CC -I$SYS_PREFIX/include \
    $cflag_additions main.c -DUSECONSOLE -o $tgt \
    -L$SYS_PREFIX/lib $ldflag_additions -lpayload -lsocket -lasound -lm"
else
  if [ `is_gui_app` = yes ]; then
    veval "$SYS_CC -I$SYS_PREFIX/include \
      $cflag_additions bootstrap.c -o $tgt \
      -L$SYS_PREFIX/lib $ldflag_additions -lpayload -lEGL -lGLESv1_CL -lbps -lscreen -lsocket -lasound -lcamapi -lm"
  else
    veval "$SYS_CC -I$SYS_PREFIX/include \
      $cflag_additions -DUSECONSOLE main.c -o $tgt \
      -L$SYS_PREFIX/lib $ldflag_additions -lpayload -lsocket -lasound -lm" 
  fi
fi
asserterror $?
assertfile $tgt
if [ ! "X$SYS_MODE" = "Xdebug" ]; then
  $SYS_STRIP $tgt
  asserterror $?
fi

rmifexists sound.assets
make_soundfile_bb10()
{
  cp $1 .
  sname=`basename $1`
  echo "<asset path=\"$sname\">$sname</asset>" >> sound.assets
}
make_sounds make_soundfile_bb10
ac_subst BB10_XML_ASSETS "@sound.assets"

echo " => preparing descriptor.."
ac_output $configsrc $configtgt
assertfile $configtgt
cat $configtgt | sed '/^#/d' > bar-descriptor.xml
rmifexists sound.assets

echo " => preparing icon.."
cp "$SYS_PREFIXROOT/build/$SYS_APPNAME/artwork-114.png" icon.png

echo " => preparing package.."
fnlfile=$SYS_PREFIXROOT/packages/$(echo $SYS_APPNAME)-$(echo $SYS_APPVERSION)-playbook.bar
if [ $SYS_MODE = release ]; then
  veval "blackberry-nativepackager -package $fnlfile bar-descriptor.xml"
  asserterror $?
  echo " => signing package.."
  veval "blackberry-signer -storepass $SYS_PLAYBOOKPW $fnlfile"
  asserterror $?
  # @@@@@ verify here
else
  token=$SYS_PREFIX/debugtoken.bar
  if [ ! -f $token ]; then
    echo " => generating debug token.."
    veval "blackberry-debugtokenrequest -storepass $SYS_PLAYBOOKPW -devicepin $SYS_PLAYBOOKDEVICEPIN $token"
    assertfile "$token" "Failed to generate debug token"
  fi
  veval "blackberry-nativepackager -package $fnlfile bar-descriptor.xml -devMode -debugToken \"$token\""
  asserterror $?
fi
cd $here

echo " => cleaning up.."
rm -rf "$tmpdir"

#eof
